package com.classic.okhttp.services;

import com.classic.okhttp.Cons;
import android.app.Activity;
import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.http.ACache;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import okhttp3.MediaType;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import com.classic.okhttp.beans.HVSignBean;


import com.classic.okhttp.enums.Enum;

public class IPayService
{

	private static String _getFullURL(String method)
	{
		return new StringBuffer( Cons.GATEWAY_URL ).append("pay/").append(method).toString();
	}

	//支付签名
	public static void sign(Activity activity, String orderId, Enum.EnumPayWay payWay, Enum.EnumItemType itemType, Boolean isWapPay, String openid, boolean isCache, ObjectCallback<String> callback)
	{
		callback.setCallBackBean(new TypeToken<String>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderId", orderId);
		params.put("payWay", payWay.value);
		params.put("itemType", itemType.value);
		params.put("isWapPay", isWapPay);
		params.put("openid", openid);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("sign");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderId).append(payWay).append(itemType).append(isWapPay).append(openid).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//支付签名
	public static void getSign(Activity activity, String orderId, Enum.EnumPayWay payWay, Enum.EnumItemType itemType, Boolean isWapPay, String openid, boolean isCache, ObjectCallback<HVSignBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVSignBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderId", orderId);
		params.put("payWay", payWay.value);
		params.put("itemType", itemType.value);
		params.put("isWapPay", isWapPay);
		params.put("openid", openid);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getSign");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderId).append(payWay).append(itemType).append(isWapPay).append(openid).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//交易查询
	public static void tradeQuery(Activity activity, String orderId, Enum.EnumPayWay payWay, boolean isCache, ObjectCallback<Object> callback)
	{
		callback.setCallBackBean(new TypeToken<Object>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderId", orderId);
		params.put("payWay", payWay.value);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("tradeQuery");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderId).append(payWay).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//申请退款
	public static void applyRefund(Activity activity, String orderId, Enum.EnumRefundType refundType, ArrayList<String> itemOrderIdList, Integer refundAmount, boolean isCache, ObjectCallback<Object> callback)
	{
		callback.setCallBackBean(new TypeToken<Object>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderId", orderId);
		params.put("refundType", refundType.value);
		params.put("itemOrderIdList", itemOrderIdList);
		params.put("refundAmount", refundAmount);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("applyRefund");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderId).append(refundType).append(itemOrderIdList).append(refundAmount).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}
}
